package com.store.framework.gui.view;

import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import com.store.framework.common.FileHandler;

/**
 * 深入学习布局
 * http://stevencjh.blog.163.com/blog/static/1218614612010101775336729/
 * 
 * @author ias
 *
 */
public class ExtParseView extends JFrame {

	private static final long serialVersionUID = 8767122696593014574L;

	JTextField jtext_path = new JTextField(22);

	JTextArea ja_text = new JTextArea(50, 60);

	JButton jbutton_refresh = new JButton("刷新");
	JButton jbutton_save = new JButton("保存");

	JTree jtree = new JTree();

	private void initData(DefaultMutableTreeNode root, String path) {
		ArrayList<String> list = FileHandler.getSimpleFileList(path);
		for (String str : list) {
			root.add(new DefaultMutableTreeNode(str));
		}

	}

	public ExtParseView() {
		jtext_path.setText("C:\\Users\\IBM_ADMIN\\Desktop\\AdminMsg");
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
		initData(root, jtext_path.getText());

		jtree = new JTree(root);

		jtree.addTreeSelectionListener(new TreeSelectionListener() {

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				TreePath path = e.getPath();
				Object[] os = path.getPath();
				String paths = jtext_path.getText() + "\\";
				for (Object o : os) {
					if (o == null || o.toString().trim().equals("")
							|| o.toString().trim().equals("root"))
						continue;

					paths += o + "\\";
				}
				JTree obj = (JTree) e.getSource();

				DefaultMutableTreeNode _obj = (DefaultMutableTreeNode) obj
						.getLastSelectedPathComponent();
				_obj.removeAllChildren();
				initData(_obj, paths);

				// 是否是文件
				String newPath = paths.substring(0, paths.length() - 1);
				StringBuffer sb = new StringBuffer();
				if (FileHandler.isFile(newPath)) {
					ArrayList<String> list = FileHandler
							.getSourcedataList(newPath);
					for (String str : list) {
						sb.append(str).append("\r\n");
					}
					ja_text.setText(sb.toString());
				}

			}
		});

		this.setTitle("Ext可视化开发工具 - IBM");
		this.add(jtext_path);
		this.add((jtree));
		this.add(scroll(ja_text));
		this.add(jbutton_refresh);
		this.add(jbutton_save);
		this.setLayout(new FlowLayout());
		this.setAlwaysOnTop(true);
		this.setVisible(true);
		this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private JScrollPane scroll(JComponent jComponent) {
		JScrollPane scroll = new JScrollPane(ja_text);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		return scroll;
	}

	public static void main(String[] args) {
		new ExtParseView();
	}
}
